package com.dims_hsrc.data.service.impl;

import com.dims_hsrc.common.utils.StringUtils;
import com.dims_hsrc.data.domain.DataKnowledgeMapping;
import com.dims_hsrc.data.domain.DataKnowledgeMappingResult;
import com.dims_hsrc.data.mapper.DataKnowledgeMappingMapper;
import com.dims_hsrc.data.service.IDataKnowledgeMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 知识映射 Service业务层处理
 *
 * @author 赵浩然
 */
@Service
public class DataKnowledgeMappingServiceImpl implements IDataKnowledgeMappingService {
    @Autowired
    private DataKnowledgeMappingMapper dataKnowledgeMappingMapper;

    @Override
    public DataKnowledgeMapping getOne(String id) {
        return dataKnowledgeMappingMapper.getOne(id);
    }

    @Override
    public List<DataKnowledgeMapping> list(DataKnowledgeMapping mapping) {
        return dataKnowledgeMappingMapper.list(mapping);
    }

    @Override
    public int add(DataKnowledgeMapping mapping) {
        if (StringUtils.isEmpty(mapping.getId())) {
            mapping.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        return dataKnowledgeMappingMapper.add(mapping);
    }

    @Override
    public int update(DataKnowledgeMapping mapping) {
        return dataKnowledgeMappingMapper.update(mapping);
    }

    @Override
    public int remove(String[] ids) {
        return dataKnowledgeMappingMapper.remove(ids);
    }

    @Override
    public int del(String id) {
        return dataKnowledgeMappingMapper.del(id);
    }

    @Override
    public List<DataKnowledgeMapping> getByDemandOptionId(String doid) {
        return dataKnowledgeMappingMapper.getByDemandOptionId(doid);
    }

    @Override
    public List<DataKnowledgeMapping> getByIndicatorOptionId(String ioid) {
        return dataKnowledgeMappingMapper.getByIndicatorOptionId(ioid);
    }
    
    @Override
    public List<DataKnowledgeMappingResult> getKnowledgeMappingsByStructId(String sid) {
        return dataKnowledgeMappingMapper.getKnowledgeMappingsByStructId(sid);
    }
    
    @Override
    public List<Map<String, Object>> getDemandOptionsBySidAndDid(Map<String, Object> params) {
        return dataKnowledgeMappingMapper.getDemandOptionsBySidAndDid(params);
    }
    
    @Override
    public List<Map<String, Object>> getIndicatorOptionsBySidAndIid(Map<String, Object> params) {
        return dataKnowledgeMappingMapper.getIndicatorOptionsBySidAndIid(params);
    }
    
    @Override
    public List<Map<String, Object>> getOptionOrRangeDemandsBySid(String sid) {
        return dataKnowledgeMappingMapper.getOptionOrRangeDemandsBySid(sid);
    }
    
    @Override
    public List<Map<String, Object>> getOptionIndicatorsBySid(String sid) {
        return dataKnowledgeMappingMapper.getOptionIndicatorsBySid(sid);
    }
    
    @Override
    public int batchAdd(List<DataKnowledgeMapping> mappingList) {
        // 为每个映射生成ID
        for (DataKnowledgeMapping mapping : mappingList) {
            if (mapping.getId() == null || mapping.getId().isEmpty()) {
                mapping.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            }
        }
        return dataKnowledgeMappingMapper.batchAdd(mappingList);
    }
    
    @Override
    public int removeByDemandId(String did) {
        return dataKnowledgeMappingMapper.removeByDemandId(did);
    }
    
    @Override
    public int removeByIndicatorId(String iid) {
        return dataKnowledgeMappingMapper.removeByIndicatorId(iid);
    }
    
    @Override
    public int removeByStructId(String sid) {
        return dataKnowledgeMappingMapper.removeByStructId(sid);
    }
    
    @Override
    public List<Map<String, Object>> getIndicatorMappingsByDemandId(String doid) {
        return dataKnowledgeMappingMapper.getIndicatorMappingsByDemandId(doid);
    }
    
    @Override
    public List<Map<String, Object>> getDemandMappingsByIndicatorId(String ioid) {
        return dataKnowledgeMappingMapper.getDemandMappingsByIndicatorId(ioid);
    }
    
    @Override
    public List<DataKnowledgeMappingResult> getKnowledgeMappingResultsBySid(String sid) {
        return dataKnowledgeMappingMapper.getKnowledgeMappingResultsBySid(sid);
    }

    /**
     * 根据需求选项ID和指标选项ID获取映射关系
     *
     * @param params 包含doid和ioid的参数Map
     * @return 映射关系列表
     */
    @Override
    public List<DataKnowledgeMappingResult> getMappingsByDemandAndIndicator(Map<String, Object> params) {
        return dataKnowledgeMappingMapper.getMappingsByDemandAndIndicator(params);
    }
    
    /**
     * 根据结构ID、需求ID和指标ID获取知识映射
     *
     * @param params 包含sid、did和iid的参数Map
     * @return 知识映射结果列表
     */
    @Override
    public List<DataKnowledgeMappingResult> getKnowledgeMappingsByStructAndDemandAndIndicator(Map<String, Object> params) {
        return dataKnowledgeMappingMapper.getKnowledgeMappingsByStructAndDemandAndIndicator(params);
    }

    /**
     * 根据需求选项ID获取知识映射结果
     *
     * @param doid 需求选项ID
     * @return 知识映射结果列表
     */
    @Override
    public List<DataKnowledgeMappingResult> getKnowledgeMappingResultsByDoid(String doid) {
        if (StringUtils.isEmpty(doid)) {
            return new ArrayList<>();
        }
        return dataKnowledgeMappingMapper.getKnowledgeMappingResultsByDoid(doid);
    }
}